<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="就诊号" prop="encounterId">
        <el-input
          v-model="queryParams.encounterId"
          placeholder="请输入就诊号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处方单号" prop="diagnoseId">
        <el-input
          v-model="queryParams.diagnoseId"
          placeholder="请输入处方单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人ID" prop="auditorId">
        <el-input
          v-model="queryParams.auditorId"
          placeholder="请输入审核人ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后审核人" prop="auditorName">
        <el-input
          v-model="queryParams.auditorName"
          placeholder="请输入最后审核人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后审核时间" prop="auditorTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.auditorTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择最后审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否需要审核" prop="isAudit">
        <el-input
          v-model="queryParams.isAudit"
          placeholder="请输入是否需要审核"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核内容" prop="auditContent">
        <el-input
          v-model="queryParams.auditContent"
          placeholder="请输入审核内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否配药" prop="isEquip">
        <el-input
          v-model="queryParams.isEquip"
          placeholder="请输入是否配药"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否发药" prop="isSend">
        <el-input
          v-model="queryParams.isSend"
          placeholder="请输入是否发药"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="isValid">
        <el-input
          v-model="queryParams.isValid"
          placeholder="请输入状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['order:log:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:log:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:log:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:log:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="就诊号" align="center" prop="encounterId" />
      <el-table-column label="处方单号" align="center" prop="diagnoseId" />
      <el-table-column label="审核人ID" align="center" prop="auditorId" />
      <el-table-column label="最后审核人" align="center" prop="auditorName" />
      <el-table-column label="最后审核时间" align="center" prop="auditorTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditorTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="auditStatus" />
      <el-table-column label="是否需要审核" align="center" prop="isAudit" />
      <el-table-column label="审核内容" align="center" prop="auditContent" />
      <el-table-column label="是否配药" align="center" prop="isEquip" />
      <el-table-column label="是否发药" align="center" prop="isSend" />
      <el-table-column label="状态" align="center" prop="isValid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:log:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:log:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改处方单操作历史对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="就诊号" prop="encounterId">
          <el-input v-model="form.encounterId" placeholder="请输入就诊号" />
        </el-form-item>
        <el-form-item label="处方单号" prop="diagnoseId">
          <el-input v-model="form.diagnoseId" placeholder="请输入处方单号" />
        </el-form-item>
        <el-form-item label="审核人ID" prop="auditorId">
          <el-input v-model="form.auditorId" placeholder="请输入审核人ID" />
        </el-form-item>
        <el-form-item label="最后审核人" prop="auditorName">
          <el-input v-model="form.auditorName" placeholder="请输入最后审核人" />
        </el-form-item>
        <el-form-item label="最后审核时间" prop="auditorTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.auditorTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择最后审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.auditStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否需要审核" prop="isAudit">
          <el-input v-model="form.isAudit" placeholder="请输入是否需要审核" />
        </el-form-item>
        <el-form-item label="审核内容" prop="auditContent">
          <el-input v-model="form.auditContent" placeholder="请输入审核内容" />
        </el-form-item>
        <el-form-item label="是否配药" prop="isEquip">
          <el-input v-model="form.isEquip" placeholder="请输入是否配药" />
        </el-form-item>
        <el-form-item label="是否发药" prop="isSend">
          <el-input v-model="form.isSend" placeholder="请输入是否发药" />
        </el-form-item>
        <el-form-item label="状态" prop="isValid">
          <el-input v-model="form.isValid" placeholder="请输入状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLog, getLog, delLog, addLog, updateLog, exportLog } from "@/api/order/log";

export default {
  name: "Log",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 处方单操作历史表格数据
      logList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        encounterId: undefined,
        diagnoseId: undefined,
        auditorId: undefined,
        auditorName: undefined,
        auditorTime: undefined,
        auditStatus: undefined,
        isAudit: undefined,
        auditContent: undefined,
        isEquip: undefined,
        isSend: undefined,
        isValid: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        encounterId: [
          { required: true, message: "就诊号不能为空", trigger: "blur" }
        ],
        diagnoseId: [
          { required: true, message: "处方单号不能为空", trigger: "blur" }
        ],
        auditorId: [
          { required: true, message: "审核人ID不能为空", trigger: "blur" }
        ],
        auditStatus: [
          { required: true, message: "状态（0 未审核 1审核通过 2审核未通过  3 从新审核，4 无需审核）不能为空", trigger: "blur" }
        ],
        isAudit: [
          { required: true, message: "是否需要审核（0 不需要审核 1 需要审核 ）不能为空", trigger: "blur" }
        ],
        isEquip: [
          { required: true, message: "是否配药（0 否  1 是 2 部分配）不能为空", trigger: "blur" }
        ],
        isSend: [
          { required: true, message: "是否发药（0 否  1 是 2 部分发 ）不能为空", trigger: "blur" }
        ],
        isValid: [
          { required: true, message: "状态（0正常 1关闭）不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询处方单操作历史列表 */
    getList() {
      this.loading = true;
      listLog(this.queryParams).then(response => {
        this.logList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        encounterId: undefined,
        diagnoseId: undefined,
        auditorId: undefined,
        auditorName: undefined,
        auditorTime: undefined,
        auditStatus: "0",
        isAudit: undefined,
        auditContent: undefined,
        isEquip: undefined,
        isSend: undefined,
        isValid: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加处方单操作历史";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改处方单操作历史";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateLog(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addLog(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除处方单操作历史编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLog(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有处方单操作历史数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportLog(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>